# Random forest
rm(list = ls())
library(tidyverse)

# Load data ----
load("D:/Code/RCode/MachLearn/prostate.RData")
str(prostate)
table(prostate$gleason)
prostate$gleason <- ifelse(prostate$gleason == 6, 0, 1)
table(prostate$gleason)

# Split train and test set ----
prostate.train <- prostate %>% filter(train == TRUE) %>% select(-train)
prostate.test <- prostate %>% filter(train == FALSE) %>% select(-train)

# Create model (Regr random forest) ----
library(mlr3)
library(mlr3viz)
pst.tsk <- TaskRegr$new(id = "prostate.rf", 
                        backend = prostate.train,
                        target = 'lpsa')

pst.rf <- lrn("regr.ranger")

pst.rf$train(pst.tsk)

pst.pred <- pst.rf$predict(pst.tsk)
pst.pred$score()

plot(pst.rf$model)


# Filter ----
library(mlr3filters)

filters <- FilterJMIM$new()
filters$calculate(pst.tsk)
print(filters)
